<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      v-dialogDrag
      append-to-body
    >
      <el-form
        ref="formRefone"
        :model="formData"
        :rules="formRules"
        v-loading="formLoading"
        label-width="100px"
      >
        <el-form-item
          label="元数据的名称,例如“使用面积”、“朝向”、“户型”等"
          prop="name"
        >
          <el-select
            v-model="formData.name"
            placeholder="请选择元数据的名称,例如“使用面积”、“朝向”、“户型”等"
          >
            <el-option
              v-for="dict in this.getDictDatas(DICT_TYPE.HOUSE_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="元数据的描述或值" prop="description">
          <el-input
            v-model="formData.description"
            placeholder="请输入元数据的描述或值"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading"
          >确定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as HouseMetaInfoApi from "@/api/system/housemetainfo";
export default {
  props: {},
  name: "HouseMetaInfoForm",
  components: {},
  data() {
    return {
      DICT_TYPE: {
        HOUSE_TYPE: "house_type", // 确保这里的值与后端接口匹配
      },

      istrue: false,
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        name: undefined,
        description: undefined,
        houseId: undefined,
      },
      // 表单校验
      formRules: {
        name: [
          {
            required: true,
            message: "元数据的名称,例如“使用面积”、“朝向”、“户型”等不能为空",
            trigger: "change",
          },
        ],
        desc: [
          {
            required: true,
            message: "元数据的描述或值不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.gethouseId();
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await HouseMetaInfoApi.getHouseMetaInfo(id);
          this.formData = res.data;

          this.title = "修改房屋元信息";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增房屋元信息";
    },
    gethouseId(house) {
      this.formData.houseId = house;
      console.log(this.formData.houseId);
    },
    /** 提交按钮 */
    async submitForm() {
      console.log(this.formData);
      // 校验主表
      await this.$refs["formRefone"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        console.log(data);
        if (this.istrue) {
          await HouseMetaInfoApi.updateHouseMetaInfo(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        console.log(this.formData.houseId);
        await HouseMetaInfoApi.createHouseMetaInfo({
          name: this.formData.name,
          houseId: this.formData.houseId,
          description: this.formData.description,
        });
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
        this.getLL();
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        name: undefined,
        desc: undefined,
      };
      this.resetForm("formRefone");
    },
  },
};
</script>
